# _*_ coding utf-8 _*_
# FILENAME：partsDetailList.py
# DESCRIPTION: 零件明细管理
# AUTH: jarvis zhang
# DATE: 2022/5/3110:53 上午
from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5.Qt import *
from sqlalchemy import desc
import qtawesome as qta

from components.BaseDetailWindow import BaseDetailWindow
from models.baseModel import get_session, delete
from models.partsModel import PartsDetailsModel
from views.bom.uploadDialog import UploadDialog
from views.bom.versionDialog import VersionDialog


class PartsDetailList(BaseDetailWindow):
    __versionId = 0
    __title = ''

    def __init__(self, versionId, title):
        super(PartsDetailList, self).__init__()
        self.__init_ui()
        self.__versionId = versionId
        self.__title = title

    ''' UI '''
    def __init_ui(self):
        print('init ui')
        # 信号槽连接
        # self.btn_add.clicked.connect(self.add_click)
        # self.btn_close.clicked.connect(self.close)

    ''' Methods '''
    #  窗体show
    def show(self):
        super(PartsDetailList, self).show()
        self.setWindowTitle("零件明细管理-" + self.__title)
        # 设置表头
        self.setTableViewHeaders()
        # 查询
        total = self.query_total()
        self.setTotalRecordCount(total)  # 总量
        self.recordQuery(0)  # page

        # 关闭窗体,释放资源
    def close(self):
        super(PartsDetailList, self).close()

    def setTableViewHeaders(self):
        self.tableeHeaderLabels = ['编号', '零件件号', '零件名称', '左右件', '装车朝向', '工厂', '卸货道口', '存储区安全库存',
                                   '存储区最大库存', '供应商名称', '包装类型', '料架是否能和托盘堆', '长', '宽', '高', 'SNP']
        self.dataModel.setHorizontalHeaderLabels(self.tableeHeaderLabels)
        # 设置列宽
        self.tableView.setColumnWidth(0, 50)
        self.tableView.setColumnWidth(1, 200)
        self.tableView.setColumnWidth(2, 150)
        self.tableView.setColumnWidth(3, 250)
        self.tableView.setColumnWidth(4, 100)
        self.tableView.setColumnWidth(5, 100)
        self.tableView.setColumnWidth(6, 100)
        self.tableView.setColumnWidth(7, 100)
        self.tableView.setColumnWidth(8, 100)
        self.tableView.setColumnWidth(9, 100)
        self.tableView.setColumnWidth(10, 100)
        self.tableView.setColumnWidth(11, 100)
        self.tableView.setColumnWidth(12, 100)
        self.tableView.setColumnWidth(13, 100)
        self.tableView.setColumnWidth(14, 100)
        self.tableView.setColumnWidth(15, 100)
        #  设置代理按钮,[btnEdit, btnUpload, btnCal, btnView, btnDel]
        # delegateBtnIsHiddes = [False, False, True, False, False]
        # self.tableView.setItemDelegateForColumn(4, CustomButtonDelegate(self, delegateBtnIsHiddes))

    # 记录查询，重写
    def recordQuery(self, limitIndex):
        data = self.page(limitIndex)
        self.dataModel.clear()
        if len(data) > 0:
            for i in range(len(data)):
                # 添加
                newItem = QStandardItem(str(data[i].id))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 0, newItem)

                newItem = QStandardItem(str(data[i].code))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 1, newItem)

                newItem = QStandardItem(str(data[i].name))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 2, newItem)

                newItem = QStandardItem(str(data[i].lr_parts))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 3, newItem)

                newItem = QStandardItem(str(data[i].loading_direction))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 4, newItem)

                newItem = QStandardItem(str(data[i].factory))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 5, newItem)

                newItem = QStandardItem(str(data[i].unloading_crossing))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 6, newItem)

                newItem = QStandardItem(str(data[i].safety_stock))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 7, newItem)

                newItem = QStandardItem(str(data[i].max_stock))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 8, newItem)

                newItem = QStandardItem(str(data[i].supplier_name))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 9, newItem)

                newItem = QStandardItem(str(data[i].packaging_type))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 10, newItem)

                newItem = QStandardItem(str(data[i].material_rack_pallet_adapted))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 11, newItem)

                newItem = QStandardItem(str(data[i].length))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 12, newItem)

                newItem = QStandardItem(str(data[i].width))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 13, newItem)

                newItem = QStandardItem(str(data[i].height))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 14, newItem)

                newItem = QStandardItem(str(data[i].snp))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 15, newItem)
        self.setTableViewHeaders()

    # 分页查询列表
    def page(self, limitIndex=0):
        # 获取dbsession
        session = get_session()
        # 拼接查询
        my_query = session.query(PartsDetailsModel)
        my_query = my_query.filter(PartsDetailsModel.is_del == 0)
        my_query = my_query.filter(PartsDetailsModel.parts_id == self.__versionId)
        # 拼接排序条件
        my_query = my_query.order_by(desc(PartsDetailsModel.update_date))
        # 分页
        my_query = my_query.offset(limitIndex).limit(self.PageRecordCount)
        # 执行查询
        model = my_query.all()
        session.close()
        return model

    # 查询数据总量
    def query_total(self):
        # 获取dbsession
        session = get_session()
        # 拼接查询
        my_query = session.query(PartsDetailsModel)
        my_query = my_query.filter(PartsDetailsModel.is_del == 0)
        my_query = my_query.filter(PartsDetailsModel.parts_id == self.__versionId)
        # 执行查询
        count = my_query.count()
        session.close()
        return count

    # 新增
    def add_click(self):
        VersionDialog.get_dialog(self)

    # 编辑，继承父类
    def edit_click(self):
        super(PartsDetailList, self).edit_click()
        button = self.sender()
        if button:
            cell = button.index
            row = cell[0]
            id = self.dataModel.item(row, 0).text()
            VersionDialog.get_dialog(self, int(id))

    #  上传，继承父类
    def upload_click(self):
        button = self.sender()
        if button:
            cell = button.index
            row = cell[0]
            id = self.dataModel.item(row, 0).text()
            UploadDialog.get_dialog(self, id)
            print(row)

    #  查看，继承父类
    def view_click(self):
        print('view')
        # button = self.sender()
        # if button:
        #     cell = button.index
        #     row = cell[0]
        #     id = self.dataModel.item(row, 0).text()
        #     dialog = BomDetailList(self, int(id))
        #     dialog.show()

    # 删除，继承父类
    def del_click(self):
        choice = QMessageBox.question(self, '', '是否删除当前数据?', QMessageBox.Yes | QMessageBox.No)  # 1
        if choice == QMessageBox.Yes:  # 2
            button = self.sender()
            if button:
                cell = button.index
                row = cell[0]
                id = self.dataModel.item(row, 0).text()
                model = PartsDetailList()
                model.id = int(id)
                delete(model)
                self.refresh()
        elif choice == QMessageBox.No:  # 4
            pass
